/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2jserver.gameserver.custom;

import java.io.File;
import java.util.StringTokenizer;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import com.l2jserver.gameserver.datatables.SkillTable;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.entity.TvTEvent;
import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
import com.l2jserver.gameserver.templates.StatsSet;

/**
 * @author Laikeriz
 */
public class CoinManager
{
	public static StatsSet buffs = new StatsSet();
	
	public static void showMainPage(L2PcInstance player)
	{
		NpcHtmlMessage html = new NpcHtmlMessage(0);
		html.setFile("data/html/coin/main.htm");
		player.sendPacket(html);
	}
	
	public static void handleCommand(L2PcInstance player, String command)
	{
		StringTokenizer tokenizer = new StringTokenizer(command, " ");
		tokenizer.nextToken();
		String actualCommand = tokenizer.nextToken();
		
		if (actualCommand.equalsIgnoreCase("buff"))
		{
			buff(player, Integer.parseInt(tokenizer.nextToken()));
		}
	}
	
	public static void buff(L2PcInstance player, int id)
	{
		if (!checkCond(player))
		{
			player.sendMessage(MessageConfigManager.getMessage("cannotBuff", player.langId).toString());
			return;
		}
		SkillTable.getInstance().getInfo(buffs.getInteger("buffId" + id), buffs.getInteger("buffLv" + id)).getEffects(player, player);
	}
	
	public static boolean checkCond(L2PcInstance player)
	{
		boolean cond = true;
		if (player.isInOlympiadMode())
			cond = false;
		if (TvTEvent.isPlayerParticipant(player.getObjectId()) && TvTEvent.isStarted())
			cond = false;
		return cond;
	}
	
	public static void init()
	{
		try
		{
			File file = new File("config/buffs.xml");
			buffs = new StatsSet();
			buffs.set("count", 0);
			Document doc = null;
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setValidating(false);
			factory.setIgnoringComments(true);
			doc = factory.newDocumentBuilder().parse(file);
			for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
			{
				if ("buffs".equalsIgnoreCase(n.getNodeName()))
				{
					for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
					{
						if ("buff".equalsIgnoreCase(d.getNodeName()))
						{
							int currentCount = buffs.getInteger("count") + 1;
							int buffId = Integer.parseInt(d.getAttributes().getNamedItem("buffId").getNodeValue());
							String buffLv = d.getAttributes().getNamedItem("buffLv").getNodeValue();
							int buffLvl = 0;
							if (buffLv.equalsIgnoreCase("max"))
								buffLvl = SkillTable.getInstance().getMaxLevel(buffId);
							else
								buffLvl = Integer.parseInt(buffLv);
							buffs.set("count", currentCount);
							buffs.set("buffId" + currentCount, buffId);
							buffs.set("buffLv" + currentCount, buffLvl);
						}
					}
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
